package com.bilin.coin.network.builder;

import com.bilin.coin.network.callback.WebSocketCallBack;
import com.bilin.coin.utils.GsonUtil;
import com.bilin.coin.utils.LogUtlis;
import com.bilin.coin.utils.SharedPreferencesUtils;

import java.util.Map;
import java.util.concurrent.TimeUnit;


import okhttp3.OkHttpClient;
import okhttp3.Request;

/**
 * Created by LG on 2018/5/1.
 */

public class WebSocketBuilder {

    private OkHttpClient mOkHttpClient;

    private Request mRequest;

    public  OkHttpClient getOkHttpClient()
    {
        mOkHttpClient = new OkHttpClient.Builder()
                .readTimeout(3000, TimeUnit.SECONDS)//设置读取超时时间
                .writeTimeout(3000, TimeUnit.SECONDS)//设置写的超时时间
                .connectTimeout(6000, TimeUnit.SECONDS)//设置连接超时时间
                .build();
        return mOkHttpClient;
    }



    public WebSocketBuilder getRequest(String url)
    {
        String cookie = SharedPreferencesUtils.getInstance().getString("Cookie", "");
        Map<String, Object> mapCookie = GsonUtil.GsonToMaps(cookie);
        StringBuffer buffer = new StringBuffer();
        if(mapCookie!=null) {
            for (String key : mapCookie.keySet()) {

                buffer.append(key).append("=").append(mapCookie.get(key)).append("; ");

            }
        }
        //headers.put("Cookie", buffer.toString() + "lang=cn;Path=/");
        String y="";
        /*if(buffer.toString().contains("JSESSIONID")){
            y=buffer.toString().replace("JSESSIONID","SESSIONID");
        }*/
        LogUtlis.e("----"+buffer.toString() + "lang=cn; Path=/");
        mRequest=new Request.Builder()
                .url(url)
                .addHeader("Cookie",buffer.toString() + "lang=cn; Path=/")
                .build();
        return this;
    }

    /**
     * 开始连接websocket
     * @param webSocketCallBack
     */
    public void newWebSocket(WebSocketCallBack webSocketCallBack)
    {
        getOkHttpClient().newWebSocket(mRequest,webSocketCallBack);
    }
}
